package com.sunyard.authorization.service.impl;

import com.sunyard.authorization.bean.FetchToken;
import com.sunyard.authorization.service.MerchantOperatorService;
import com.sunyard.constant.ErrMessageConst;
import com.sunyard.dal.entity.Operator;
import com.sunyard.dal.entity.Staff;
import com.sunyard.dal.mapper.StaffMapper;
import com.sunyard.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class MerchantOperatorServiceImpl implements MerchantOperatorService {

    @Autowired
    StaffMapper staffMapper;

    @Override
    public void login(HttpServletResponse response, HttpServletRequest request, String loginName, String loginPwd) throws Exception {
        Staff staff = staffMapper.selectByNameAndPwd(loginName, loginPwd);
        if (staff == null) { //输入账号或密码错误 ,5次机会
/*        	staff = staffMapper.selectByMchntCd(loginName);
        	if (staff == null) {
        		staff = staffMapper.selectByLoginName(loginName);
        	}*/
            staff = staffMapper.selectByLoginName(loginName);
            //输入账号或密码错误 ,每天5次机会
            if (staff != null && (5 - staff.getPwdErrorNum()) == 0) {
                //锁定状态的到第二天,自动解锁
                String updateDateTime = staff.getUpdateDatetime();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String subupdate = updateDateTime.substring(0, 10);
                String endupdate = subupdate + " 23:59:59";
                Date endupdate1 = sdf.parse(endupdate);
                //    Date updateDateTime1 = sdf.parse(updateDateTime);
                String now = sdf.format(new Date());
                Date now1 = sdf.parse(now);
                //   long day = (now1.getTime() - updateDateTime1.getTime())/(24*60*60*1000);
                /*Date date1 = DateUtil.getDate(DateUtil.YYYYMMDDHHMMSS, updateDateTime);
                String updateDateTime1 = DateUtil.getDateStr(DateUtil.YYYYMMDD, date1);
                String now = DateUtil.getDateStr(DateUtil.YYYYMMDD, new Date());//当天*/
                if (now1.after(endupdate1)) {
                    staff.setUpdateDatetime(DateUtil.getDefaultDateStr(new Date()));//当天时分秒
                    staff.setPwdErrorNum(1);
                    staff.setStatus("1");
                    staffMapper.updateByPrimaryKeySelective(staff);
                    throw new IllegalAccessException("用户名或密码错误,还有4次输入机会");
                }
                throw new IllegalAccessException("账号已被锁定");
            } else if (staff != null && (5 - staff.getPwdErrorNum()) > 0) {
                staff.setPwdErrorNum(staff.getPwdErrorNum() + 1);
                if ((5 - staff.getPwdErrorNum()) == 0) {
                    staff.setStatus("4");//锁定状态
                    staff.setUpdateDatetime(DateUtil.getDefaultDateStr(new Date()));//记录锁定时间
                    staffMapper.updateByPrimaryKeySelective(staff);
                    throw new IllegalAccessException("账号被锁定");
                }
                staffMapper.updateByPrimaryKeySelective(staff);//错误次数 +1
                throw new IllegalAccessException("用户名或密码错误,还有" + (5 - staff.getPwdErrorNum())
                        + "次输入机会");
            } else if (staff == null) {
                throw new IllegalAccessException(ErrMessageConst.USERNAME_NOT_EXIST);
            }
        }
        if (staff.getUserType() == 3) {
            throw new IllegalAccessException("店员无权限获取!");
        }
    }

    @Override
    public Operator getOperatorByOpName(FetchToken fetchToken) {
        return null;
    }
}
